<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V1_8_8;

use App\Http\Requests\Api\V1_8_8\CreditCardUpdateRequest;
use App\Http\Requests\Api\V1_8_8\IdentityImageRequest;
use App\Http\Requests\Api\V1_8_8\IdentityMessageBankCardBindRequest;
use App\Http\Requests\Api\V1_8_8\IdentityMessageCreditCardBindRequest;
use App\Http\Requests\Api\V1_8_8\IdentityMessageRequest;
use App\Services\CreditCardService;
use App\Services\Xinyan\IdentityCardService;
use Illuminate\Http\Request;

class IdentityController extends BaseController
{
    protected $identityCardService;

    public function __construct(IdentityCardService $identityCardService)
    {
        parent::__construct();
        $this->identityCardService = $identityCardService;
    }

    /**
     * 身份证与银行卡认证
     * @param IdentityMessageRequest $request
     * @param IdentityCardService $identityCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function identityAuth(IdentityMessageRequest $request)
    {
        if (1 == $this->user['is_truename_auth']) {
            return $this->responseJson(10300);//已通过实名认证
        }
        $isPhoto = 'noPhoto';
        $verifyElement = '1234';//12:两要素(银行卡号 + 姓名) 123:三要素(银行卡号 + 姓名 + 身份证号) 1234:四要素(银行卡号 + 姓名 + 身份证号 + 银行卡预留手机号)
        $industryType = 'A6';//行业类别 A6 安全、工具类产品
        $productType = 0;
        return $this->identityCardService->identityAuth($this->user['user_id'], $request->verification_code,
            str_replace(' ', '', $request->id_card),
            $isPhoto, str_replace(' ', '', $request->bank_no),
            $request->mobile, $verifyElement, removeSpaceString($request->true_name), $industryType, $productType);
    }

    /**
     * 身份认证上传图片
     * @param IdentityImageRequest $request
     * @return bool|\Illuminate\Http\JsonResponse|string
     */
    public function identityUpload(IdentityImageRequest $request)
    {
        return $this->identityCardService->identityUpload($request, $this->user['user_id']);
    }

    /**
     * 添加银行卡
     * @param IdentityMessageBankCardBindRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bindBankCard(IdentityMessageBankCardBindRequest $request)
    {
        $verifyElement = '1234';//12:两要素(银行卡号 + 姓名) 123:三要素(银行卡号 + 姓名 + 身份证号) 1234:四要素(银行卡号 + 姓名 + 身份证号 + 银行卡预留手机号)
        $industryType = 'A6';//行业类别 A6 安全、工具类产品
        $productType = 0;
        return $this->identityCardService->bindBankCard($this->user, $request->verification_code,
            str_replace(' ', '', $request->bank_no), $request->mobile, $verifyElement, $industryType, $productType);
    }

    /**
     * 添加信用卡
     * @param IdentityMessageCreditCardBindRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bindCreditCard(IdentityMessageCreditCardBindRequest $request)
    {
        $verifyElement = '1234';//12:两要素(银行卡号 + 姓名) 123:三要素(银行卡号 + 姓名 + 身份证号) 1234:四要素(银行卡号 + 姓名 + 身份证号 + 银行卡预留手机号)
        $industryType = 'A6';//行业类别 A6 安全、工具类产品
        $productType = 0;
        $repaymentMoney = isset($request->repayment_money) ? $request->repayment_money : 0;
        return $this->identityCardService->bindCreditCard($this->user, $request->verification_code,
            str_replace(' ', '', $request->bank_no), $request->mobile, $verifyElement,
            $industryType, $productType, $request->bill_day, $request->repayment_day, $request->credit_lines, $repaymentMoney);
    }
}